package com.wcgksoft.controller.bigdata;


import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.Patrol.PatrolAccount;
import com.wcgksoft.entity.Plant.PlantType;
import com.wcgksoft.entity.insect.InsectHistory;
import com.wcgksoft.entity.mainten.Mainten;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.Patrol.PatrolAccountService;
import com.wcgksoft.service.insect.InsectHistoryService;
import com.wcgksoft.service.mainten.MaintenService;
import com.wcgksoft.service.plant.PlantTypeService;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping(value = "bigdata")
public class BigDataController {


    @Autowired
    private PatrolAccountService patrolAccountService;
    @Autowired
    private MaintenService maintenService;
    @Autowired
    private InsectHistoryService insectHistoryService;
    @Autowired
    private GreedlandService greedlandService;
    @Autowired
    private PlantTypeService plantTypeService;


    @RequestMapping
    public String bigdata(ModelMap modelMap){

        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject project = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("project");
        if (null!=project){
            //领导角色切换项目id
            Integer id = project.getId();

            DetachedCriteria patrolAccount = DetachedCriteria.forClass(PatrolAccount.class);
            patrolAccount.add(Restrictions.eq("proId", id));
            patrolAccount.add(Restrictions.eq("status", "0"));
            List<PatrolAccount> patrolAccountList = patrolAccountService.findByConditions(patrolAccount);
            modelMap.put("patrolAccountNumber", patrolAccountList.size());
            System.out.println("巡查次数--------------------------------------------------"+patrolAccountList.size());

            DetachedCriteria mainten = DetachedCriteria.forClass(Mainten.class);
            mainten.add(Restrictions.eq("departid", String.valueOf(id)));
            List<Mainten> maintenList = maintenService.findByConditions(mainten);
            modelMap.put("maintenNumber", maintenList.size());
            System.out.println("养护次数--------------------------------------------------"+maintenList.size());

            DetachedCriteria insectHistory = DetachedCriteria.forClass(InsectHistory.class);
            insectHistory.add(Restrictions.eq("departId", id));
            List<InsectHistory> insectHistoryList = insectHistoryService.findByConditions(insectHistory);
            modelMap.put("insectHistoryNumber", insectHistoryList.size());
            System.out.println("病虫次数--------------------------------------------------"+insectHistoryList.size());

            DetachedCriteria greedLand = DetachedCriteria.forClass(GreedLand.class);
            greedLand.add(Restrictions.eq("departid", String.valueOf(id)));
            List<GreedLand> greedLandList = greedlandService.findByConditions(greedLand);
            Integer number = 0;
            if (greedLandList.size()>0){
                for (GreedLand land : greedLandList) {
                    number += land.getGreenArea();
                }
            }
            modelMap.put("GreedLandNumber", number);
            System.out.println("绿化面积--------------------------------------------------"+number);

            DetachedCriteria plantType = DetachedCriteria.forClass(PlantType.class);
            plantType.add(Restrictions.eq("departid", String.valueOf(id)));
            plantType.add(Restrictions.eq("state", 0));
            List<PlantType> plantTypeList = plantTypeService.findByConditions(plantType);
            modelMap.put("plantTypeNumber", plantTypeList.size());
            System.out.println("植物种类--------------------------------------------------"+plantTypeList.size());



            DetachedCriteria mainten1 = DetachedCriteria.forClass(Mainten.class);
            mainten1.add(Restrictions.eq("departid", String.valueOf(id)));
            mainten1.add(Restrictions.eq("type", 1));
            List<Mainten> maintenList1 = maintenService.findByConditions(mainten1);
            modelMap.put("mainten1Number", maintenList1.size());

            DetachedCriteria mainten2 = DetachedCriteria.forClass(Mainten.class);
            mainten2.add(Restrictions.eq("departid", String.valueOf(id)));
            mainten2.add(Restrictions.eq("type", 2));
            List<Mainten> maintenList2 = maintenService.findByConditions(mainten2);
            modelMap.put("mainten2Number", maintenList2.size());

            DetachedCriteria mainten3 = DetachedCriteria.forClass(Mainten.class);
            mainten3.add(Restrictions.eq("departid", String.valueOf(id)));
            mainten3.add(Restrictions.eq("type", 3));
            List<Mainten> maintenList3 = maintenService.findByConditions(mainten3);
            modelMap.put("mainten3Number", maintenList3.size());

            DetachedCriteria mainten4 = DetachedCriteria.forClass(Mainten.class);
            mainten4.add(Restrictions.eq("departid", String.valueOf(id)));
            mainten4.add(Restrictions.eq("type", 4));
            List<Mainten> maintenList4 = maintenService.findByConditions(mainten4);
            modelMap.put("mainten4Number", maintenList4.size());



        }else{

            DetachedCriteria patrolAccount = DetachedCriteria.forClass(PatrolAccount.class);
            patrolAccount.add(Restrictions.eq("status", "0"));
            List<PatrolAccount> patrolAccountList = patrolAccountService.findByConditions(patrolAccount);
            modelMap.put("patrolAccountNumber", patrolAccountList.size());
            System.out.println("巡查次数--------------------------------------------------"+patrolAccountList.size());

            DetachedCriteria mainten = DetachedCriteria.forClass(Mainten.class);
            List<Mainten> maintenList = maintenService.findByConditions(mainten);
            modelMap.put("maintenNumber", maintenList.size());
            System.out.println("养护次数--------------------------------------------------"+maintenList.size());

            DetachedCriteria insectHistory = DetachedCriteria.forClass(InsectHistory.class);
            List<InsectHistory> insectHistoryList = insectHistoryService.findByConditions(insectHistory);
            modelMap.put("insectHistoryNumber", insectHistoryList.size());
            System.out.println("病虫次数--------------------------------------------------"+insectHistoryList.size());

            DetachedCriteria greedLand = DetachedCriteria.forClass(GreedLand.class);
            List<GreedLand> greedLandList = greedlandService.findByConditions(greedLand);
            Integer number = 0;
            if (greedLandList.size()>0){
                for (GreedLand land : greedLandList) {
                    number += land.getGreenArea();
                }
            }
            modelMap.put("GreedLandNumber", number);
            System.out.println("绿化面积--------------------------------------------------"+number);

            DetachedCriteria plantType = DetachedCriteria.forClass(PlantType.class);
            plantType.add(Restrictions.eq("state", 0));
            List<PlantType> plantTypeList = plantTypeService.findByConditions(plantType);
            modelMap.put("plantTypeNumber", plantTypeList.size());
            System.out.println("植物种类--------------------------------------------------"+plantTypeList.size());



            DetachedCriteria mainten1 = DetachedCriteria.forClass(Mainten.class);
            mainten1.add(Restrictions.eq("type", 1));
            List<Mainten> maintenList1 = maintenService.findByConditions(mainten1);
            modelMap.put("mainten1Number", maintenList1.size());

            DetachedCriteria mainten2 = DetachedCriteria.forClass(Mainten.class);
            mainten2.add(Restrictions.eq("type", 2));
            List<Mainten> maintenList2 = maintenService.findByConditions(mainten2);
            modelMap.put("mainten2Number", maintenList2.size());

            DetachedCriteria mainten3 = DetachedCriteria.forClass(Mainten.class);
            mainten3.add(Restrictions.eq("type", 3));
            List<Mainten> maintenList3 = maintenService.findByConditions(mainten3);
            modelMap.put("mainten3Number", maintenList3.size());

            DetachedCriteria mainten4 = DetachedCriteria.forClass(Mainten.class);
            mainten4.add(Restrictions.eq("type", 4));
            List<Mainten> maintenList4 = maintenService.findByConditions(mainten4);
            modelMap.put("mainten4Number", maintenList4.size());

        }




        return "bigdata/list";
    }



}
